package cn.com.headfree.evaluation.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TDeclareEvaluationInfo;
import cn.com.headfree.entity.TDeclareEvaluationPoint;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.evaluation.export.EvaluationDeclareExportWordService;
//import cn.com.headfree.evaluation.export.EvaluationDeclareUserDataInfoService;
import cn.com.headfree.evaluation.export.EvaluationDeclareUserDataInfoService;
import cn.com.headfree.evaluation.export.vo.EvaluationDeclareReportVo;
import cn.com.headfree.evaluation.service.IDeclareEvaluationService;
import cn.com.headfree.evaluation.service.IEvaluationPointManageService;
import cn.com.headfree.expert.export.DeclareExpertUserDataInfoService;
import cn.com.headfree.util.ExcelExportUtil;
import cn.com.headfree.vo.TDeclareEvaluationPointVo;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *评价积分管理
 */
@Controller
@RequestMapping(value = "/evaluationPointManage", produces = "application/json;charset=UTF-8")
public class EvaluationPointManageController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(EvaluationPointManageController.class);

	@Autowired
	private IEvaluationPointManageService evaluationPointManageService;

	@Autowired
	private IDeclareEvaluationService declareEvaluationService;

	@Autowired
	private EvaluationDeclareExportWordService evaluationDeclareExportWordService;

	@Autowired
	private EvaluationDeclareUserDataInfoService evaluationDeclareUserDataInfoService;


	/**
	 * 积分管理分页查询
	 */
	@RequestMapping("/findDeclareEvaluationPointPageList")
	@ResponseBody
	public ResultData findDeclareEvaluationPointPageList(HttpServletRequest request, Page<TDeclareEvaluationPointVo> page, TDeclareEvaluationPointVo teclareEvaluationPoint){
		ResultData result = new ResultData();
		try{
			User user = this.getLoginUserInfo(request);
			teclareEvaluationPoint.setOrgId(user.getBaseOrgId());
			evaluationPointManageService.findDeclareEvaluationPointPageList(page,teclareEvaluationPoint);
			result.setSuccess(true);
			result.setData(page);
			LOG.info("查询积分管理成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("查询失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 导出报表
	 */
	@GetMapping("/exportDeclareEvaluationPointExcel")
	public void exportDeclareEvaluationPointExcel(HttpServletRequest request, HttpServletResponse response, TDeclareEvaluationPointVo teclareEvaluationPoint) {
		try {
			User user = this.getLoginUserInfo(request);
			//查询要导出的报表数据
			teclareEvaluationPoint.setOrgId(user.getBaseOrgId());
			List<TDeclareEvaluationPointVo> list = evaluationPointManageService.findDeclareEvaluationPointList(teclareEvaluationPoint);
			String personYearPointDetailHead = "部门,姓名,ERP编号,年度,履职贡献,教育培训,导师带徒,个人荣誉,成果获奖,技艺革新,授权专利,论文著作,制度标准,技术标准,合计";
			//获取标题
			String[] personYearPointDetailHeads = personYearPointDetailHead.split(",");
			//sheet名称
			String sheetName = "人才评价积分报表导出";
			//获取数据
			String[][] Content = ExcelExportUtil.makePersonYearEvaluationPointDetailHead(list, personYearPointDetailHeads);
			//创建workbook
			XSSFWorkbook workbook = new XSSFWorkbook();
			workbook = ExcelExportUtil.exportExcel(workbook, personYearPointDetailHeads, Content, sheetName);//基本信息
			//响应生成excel
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
			LOG.info("开始导出excel报表");
			ExcelExportUtil.exportExcel2Browser(response, sheetName + sdf.format(new Date()), workbook);
			LOG.info("导出excel报表成功");
		} catch (Exception e) {
			LOG.error("导出失败");
			e.printStackTrace();
		}
	}

	/**
	 * 普通人员提交申报--保存积分明细,如果已经申报提交，则删除之前的积分明细
	 * @param request
	 * @param declareEvaluationPoint
	 * @return
	 */
	@RequestMapping("/saveDeclareEvaluationPoint" )
	@ResponseBody
	public ResultData saveDeclareEvaluationPoint(HttpServletRequest request, @RequestBody TDeclareEvaluationPoint declareEvaluationPoint){
		ResultData result = new ResultData();
		try{
			if(StringUtil.isNullOrEmpty(declareEvaluationPoint.getDeclareId())){
				result.setErrorMsg("人才评价申报ID不能为空");
				result.setSuccess(false);
				return result;
			}
			User user = this.getLoginUserInfo(request);
			TDeclareEvaluationInfo declareEvaluationInfo = declareEvaluationService.getTDeclareEvaluationInfoById(declareEvaluationPoint.getDeclareId());
			if("1".equals(declareEvaluationInfo.getAuditStatus())){
				result.setSuccess(false);
				result.setErrorMsg("该提报已通过审核，不可再次提交！");
				return result;
			}
			if(!declareEvaluationInfo.getErpNo().equals(user.getFax())){
				result.setSuccess(false);
				result.setErrorMsg("只能提交本人信息，请尝试退出后重新登录！");
				return result;
			}
			//只能申报上年度，如果不是，驳回
			Calendar date = Calendar.getInstance();
			int currentYear = Integer.valueOf(date.get(Calendar.YEAR));
			int appYear = currentYear ;
			if(declareEvaluationPoint.getYear().equals(appYear)){
				this.evaluationPointManageService.saveInfo(declareEvaluationPoint,user);
				result.setData("保存成功");
				result.setSuccess(true);
			}else{
				result.setData("无法申报该年度，只能申报当前年度");
				result.setSuccess(false);
			}
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("保存失败");
			LOG.error("人才评价申报-{}",e);
		}
		return result;
	}

	/**
	 * 管理员调整积分
	 * @param request
	 * @param declareEvaluationPoint
	 * @return
	 */
	@RequestMapping("/updateDeclareEvaluationPoint" )
	@ResponseBody
	public ResultData updateDeclareEvaluationPoint(HttpServletRequest request, @RequestBody TDeclareEvaluationPoint declareEvaluationPoint){
		ResultData result = new ResultData();
		try{
			if(StringUtil.isNullOrEmpty(declareEvaluationPoint.getId())){
				result.setErrorMsg("积分明细主键不能为空");
				result.setSuccess(false);
				return result;
			}
			User user = this.getLoginUserInfo(request);
			this.evaluationPointManageService.updateEvaluationPoint(declareEvaluationPoint,user);
			result.setData("修改成功");
			result.setSuccess(true);
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("保存失败");
			LOG.error("人才评价申报-{}",e);
		}
		return result;
	}

	/**
	 * 个人积分页面查询
	 */
	@RequestMapping("/getDeclareEvaluationPoint")
	@ResponseBody
	public ResultData getDeclareEvaluationPoint(HttpServletRequest request, String year){
		ResultData result = new ResultData();
		try{
			User user = this.getLoginUserInfo(request);
			TDeclareEvaluationPointVo declareEvaluationPointVo = evaluationPointManageService.getDeclareEvaluationPoint(year,user.getBaseOrgId(),user.getFax());
			result.setSuccess(true);
			result.setData(declareEvaluationPointVo);
			LOG.info("查询个人积分详情成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("查询个人积分详情失败");
			e.printStackTrace();
		}
		return result;
	}



	/**
	 * 人才评价材料导出到word
	 * @param response
	 */
	@GetMapping("/exportWord")
	public void exportWord(HttpServletRequest request,HttpServletResponse response,String declareId,Date acquTime){
		User user = this.getLoginUserInfo(request);
		try {
			if (StringUtil.isNullOrEmpty(user.getFax())){
				LOG.error("获取当前登录人人资编号失败");
				return;
			}
			LOG.info("人才评价材料导出word,查询数据开始");
			//获取待导出的数据
			EvaluationDeclareReportVo modelData = evaluationDeclareUserDataInfoService.getModelData(user.getFax(),declareId,acquTime);
			LOG.info("信息导出word,查询数据完成,开始导出word");
			//响应页面
			evaluationDeclareExportWordService.exportWord(response, modelData,user.getName()+"人才评价材料导出",user.getFax());
			LOG.info("个人信息导出word,导出word完成");
		}catch (Exception e){
			e.printStackTrace();
			LOG.error("个人信息导出word异常!!!",e);
		}
	}


	/**
	 * 积分查询
	 */
	@RequestMapping("/selectPointByDeclareId")
	@ResponseBody
	public ResultData selectPointById(HttpServletRequest request, String declareId){
		ResultData result = new ResultData();
		try{
			TDeclareEvaluationPointVo declareEvaluationPointVo = evaluationPointManageService.selectTDeclareEvaluationPointById(declareId);
			result.setSuccess(true);
			result.setData(declareEvaluationPointVo);
			LOG.info("查询个人积分详情成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("查询个人积分详情失败");
			e.printStackTrace();
		}
		return result;
	}


	/**
	 *
	 * @param request
	 * @param response
	 * @param sysCode
	 * @param businessNode
	 * @param salaryId
	 * @param year
	 */
//	@RequestMapping("/downloadAllFiles")
//	public void downloadAllFiles(HttpServletRequest request, HttpServletResponse response, String sysCode,
//								 String businessNode, String salaryId, int year) {
//		ResultData rs = new ResultData();
//		try {
//			User user = this.getLoginUserInfo(request);
//			salaryService.downloadFiles(user.getFax(), sysCode, businessNode, request, response, salaryId, year);
//			LOG.info("下载附件成功！");
//		} catch (Exception e) {
//			rs.setSuccess(false);
//			rs.setErrorMsg("下载附件失败！");
//			e.printStackTrace();
//		}
//	}

}
